<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Code by TinhNQ
 * 2014
 **/
class Dish extends Admin_Controller {

    function __construct(){
        parent::__construct();
        $this->admin_navigation->parent_active('dish');
        $this->load->library('session');
        $this->load->model('dish/dish_model');
        $this->load->model('restaurant/menus_model');
    }

    function manager_dish() {
        $columns = array(
					array(
					'name'        => 'ID',
					'type'        => 'id',
					'index'       => 'dish_id',
					'filter'      => 'dish_id',
					'sort_column' => true,
					),
					array(
					'name'        => 'Món ăn',
					'type'        => 'text',
					'index'       => 'name',
					'filter'      => 'name',
					'sort_column' => true,
					),
					array(
					'name'        => 'Đặc sắc',
					'type'        => 'publish',
					'index'       => 'is_hot',
					'filter'      => 'is_hot',
					'sort_column' => true,
					'options'     => array('0' => 'Không', '1' => 'Có'),
					),
					array(
					'name'        => 'Trạng thái',
					'type'        => 'publish',
					'index'       => 'status',
					'filter'      => 'status',
					'sort_column' => true,
					'options'     => array('0' => 'Không', '1' => 'Có', '2' => 'Chưa duyệt'),
					),
				);
        $this->grid->set_limit(100);
        $this->grid->columns($columns);
        $this->grid->datasource('dish/dish_model','get_dishs');
        $this->grid->set_title('Quản lý món ăn');
        $this->grid->add_button('Thêm mới',admin_url('*/dish/add_dish'),'add');
        $this->grid->set_row_action('edit_dish');
        $this->grid->total_rows($this->dish_model->get_count());
        $this->grid->add_mass_action('delete', 
				array(
				'label'   =>'Xóa',
				'url'     => admin_url('*/dish/delete_dish'),
				'confirm' => 'Are you sure?'
				));
        $this->grid->add_mass_action('status_on', 
				array(
				'label' =>'Thay đổi trạng thái -> Bật',
				'url'   => admin_url('*/dish/change_status_dish_on'),
				));
		$this->grid->add_mass_action('status_off', 
				array(
				'label' =>'Thay đổi trạng thái -> Tắt',
				'url'   => admin_url('*/dish/change_status_dish_off'),
				));
        if ($this->input->is_ajax()){
            $this->output->set_output($this->grid->display());
        }else{
            $this->load->view('admin/grid');
        }
    }

    function edit_dish() {
    	error_reporting(E_ALL & ~E_NOTICE);
        $dish_id = $this->input->get_param('id', false);
        $data = array();

        if ($dish_id) {
            $data = $this->dish_model->get_dish($dish_id);
	        $data['category_ids'] = $this->dish_model->get_category_dish($dish_id);
			$data['stocks'] = $this->dish_model->get_stocks($dish_id);
			$data['steps'] = $this->dish_model->get_steps($dish_id);
        }
		
		$select = array();
		$select['is_hot'] = $this->echo_select_form(
			array(
				'name'     => 'is_hot',
				'id'       => 'is_hot',
				'required' => TRUE,
				'onchange' => '',
				'values'   => array('Tắt', 'Bật'),
				'value'    => $data['is_hot'],
			));
		$select['status'] = $this->echo_select_form(
			array(
				'name'     => 'status',
				'id'       => 'status',
				'required' => TRUE,
				'onchange' => '',
				'values'   => array('Tắt', 'Bật', '2' => 'Chưa duyệt'),
				'value'    => $data['status'],
			));
		$categories = $this->dish_model->get_categories();
		
		$action = admin_url('*/dish/edit_post_dish' , array('id' => $dish_id));

        $this->load->view('admin/dish', array('form' => $this->form->to_html(), 
		'data' => $data, 'select' => $select, 'action' => $action, 'categories' => $categories));
    }

    function add_dish() {
        $this->_forward('edit_dish');
    }

    function delete_dish() {
        $cids = explode(',', $this->input->get_param('dish_id',false));

        foreach ($cids as $cid) {
            $this->dish_model->delete_dish($cid);
        }
        $this->_redirect(admin_url('*/dish/manager_dish'));
    }

	function change_status_dish_on() {
        $cids = explode(',', $this->input->get_param('dish_id',false));

        foreach ($cids as $cid) {
            $this->dish_model->change_status_dish($cid, 1);
        }
        $this->_redirect(admin_url('*/dish/manager_dish'));
    }
	
    function change_status_dish_off() {
        $cids = explode(',', $this->input->get_param('dish_id',false));

        foreach ($cids as $cid) {
            $this->dish_model->change_status_dish($cid, 0);
        }
        $this->_redirect(admin_url('*/dish/manager_dish'));
    }

    function edit_post_dish() {
        $dish_id = $this->input->get_param('id', false);

        if (($data = $this->input->post())) {
			$config['upload_path']   = './images/content/step/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['encrypt_name']  = 'TRUE';
			
            $this->load->library('upload', $config);
			if ($this->upload->do_upload('image1')) {
                $data['image1'] = $config['upload_path'] . $this->upload->get_file_name();
            }
			
			for($i = 0; $i < 12; $i++) {
				$nameComment = 'step_' . $i;
				$filePhoto   = 'filePhoto_' . $i;
				$h_filePhoto = 'h_filePhoto_' . $i;
				
				if (!$data[$nameComment])
				{
					continue;
				}
				$data['steps'][$i]['comment'] = $data[$nameComment];	
				$data['steps'][$i]['dish_id'] = $dish_id;
				$data['steps'][$i]['image']   = $data[$h_filePhoto];
				
				if ($this->upload->do_upload($filePhoto)) {
					$filename = $config['upload_path'] . $this->upload->get_file_name();					
					$imgData = $this->upload->data();
					parent::watermark($imgData['full_path']);
	                $data['steps'][$i]['image'] = $filename;
	            }
			}

            if ($dish_id) {
                $this->dish_model->update_dish($data, $dish_id);
            } else {
                $this->dish_model->insert_dish($data);
            }
            $this->_redirect(admin_url('*/dish/manager_dish'));
        }
    }
	
	function echo_select_form($_elements)
	{
		$multiple = '';
		if (isset($_elements['multiple'])) {
			$multiple = 'size="10" multiple="multiple"';
		}
		
		if (isset($_elements['required'])) {
			$class ="required-entry";
		}

		$onchange = '';
		if (isset($_elements['onchange'])) {
		    $onchange = " onchange='{$_elements['onchange']}' ";
		}

		$return ="<select id=\"{$_elements['id']}\" name=\"{$_elements['name']}\"
		 class=\"select {$class}\" {$onchange} {$multiple}>\n";

		$return .="<option value=\"\"></option>\n";
		if (is_array($_elements['values']) && count($_elements['values'] > 0)){

			foreach ($_elements['values'] as $_value=>$_label){
				$slected = "";
				if ($_elements['value'] == $_value){
					$slected = "selected=\"selected\"";
				}
				$return .="<option value=\"{$_value}\" $slected>{$_label}</option>\n";
			}
		}
		$return .="</select>\n";
		
		return $return;
	}

	function category_dish() {
		$columns = array(
					array(
						'name'        => 'ID',
						'type'        => 'id',
						'index'       =>'category_id',
						'sort_column' =>'desc'
						),
					array(
						'name'        =>$this->__('Name'),
						'type'        =>'text',
						'filter'      =>'name',
						'index'       =>'treename',
						'sort_column' =>true
					),
					array(
						'name'  =>$this->__('Created'),
						'type'  =>'date',
						'index' =>'created',
					),
					array(
						'name'    =>$this->__('Status'),
						'type'    =>'publish',
						'filter'  =>'status',
						'index'   =>'status',
						'action'  =>'publishcategory',
						'options' =>array(
							'0'       =>'Tắt',
							'1'       =>'Bật'
						)
					),
		);
		$this->grid->columns($columns);
		$this->grid->set_limit(100);
		$this->grid->datasource('dish/dish_model','get_categories');
		$this->grid->set_title('Category Dish');
		$this->grid->add_button('Thêm mới',admin_url('*/dish/add_category'),'add');
		$this->grid->set_row_action('edit_category');
		$this->grid->add_mass_action('delete',array(
												'label'   =>'Delete',
												'url'     => admin_url('*/dish/delete_category'),
												'confirm' => 'Are you sure?'
											));
		$this->grid->add_mass_action('status_on',array(
												'label' =>'Status => On',
												'url'   =>admin_url('*/dish/change_status_on_category'),
											));
		$this->grid->add_mass_action('status_off',array(
												'label' =>'Status => Off',
												'url'   =>admin_url('*/dish/change_status_off_category'),
											));
		$this->load->model('dish/dish_model');
		$total_rows = $this->dish_model->get_count_categories(array(),true);
		$this->grid->total_rows($total_rows);
		
		if ($this->input->is_ajax()){
			$this->output->set_output($this->grid->display());
		}else{
			$this->load->view('admin/grid');
		}
	}
	
	function edit_category(){
		$cat_id = $this->input->get_param('id',false);
		$this->load->model('dish/dish_model');
		if ($cat_id){
			$this->load->model('categories/categories_model');
			$data = $this->categories_model->get_category($cat_id);
			$this->form->set_values($data);
		}
		$this->form->set_form_attributes(array(
			'id'      =>'edit_form',
			'action'  =>admin_url('*/dish/edit_post_category',array('id'=>$cat_id)),
			'enctype' =>'multipart/form-data'
		));
		$this->form->add_filedset('ganeral',array('legend'=>'Ganeral'));
		
		$this->form->add_filed('name','text',array(
			'label'    =>'Name',
			'name'     =>'name',
			'required' =>true
		));
		
		$this->form->add_filed('alias','alias',array(
			'label'    =>'Alias',
			'name'     =>'alias',
			'required' =>false,
		));
		
		$this->form->add_filed('parent_id','select',array(
			'label'    =>'Parent',
			'name'     =>'parent_id',
			'required' =>true,
			'values'   => $this->dish_model->get_categories_options(array('other_category_id'=>$cat_id)),
		));
		$this->form->add_filed('image','image',array(
			'label' =>'Image',
			'name'  =>'image',
		));
		$this->form->add_filed('status','select',array(
			'label'    =>'Status',
			'name'     =>'status',
			'values'   =>array('Tắt','Bật'),
			'required' =>true
		));
		$this->form->add_filed('description','editor',array(
			'label' =>'Description',
			'name'  =>'description'
		));

		
		$data = array('form_title'=>'Add Category','form'=>$this->form->to_html());
		$this->load->view('admin/form',$data);
	}
	
	function add_category(){
		$this->_forward('edit_category');
	}
	
	function edit_post_category(){
	 	$cat_id = $this->input->get_param('id', false);
	 	$this->load->model('categories/categories_model');
	 	if ($cat_id){
	 		$this->categories_model->update_category($cat_id, $this->input->post());
	 	}else{
			$data = array_merge(array('module'=>'dish'), $this->input->post());
			$this->categories_model->insert_category($data);
	 	}
		$this->_redirect(admin_url('*/dish/category_dish'));
	}
	
	function delete_category() {
		$cid = explode(',', $this->input->get_param('category_id'));
		if (count($cid)>0) {
			foreach ($cid as $id){
				$this->load->model('dish/dish_model');
				$this->dish_model->delete_category($id);
			}
		}
		$this->_redirect(admin_url('*/dish/category_dish'));
	}

	function change_status_category($change) {
	    $cids = explode(',', $this->input->get_param('category_id', false));
	    $this->load->model('categories/categories_model');
	    foreach ($cids as $cid) {
	        $this->categories_model->change_status($cid, $change);
	    }
	    $this->_redirect(admin_url('*/dish/category_dish'));
	}

	function change_status_on_category() {
	    $this->change_status_category(1);
	}

	function change_status_off_category() {
	    $this->change_status_category(0);
	}
}
?>